package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.Destination;
import com.eling.elcms.travel.service.IDestinationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class DestinationController extends BaseFormController{
	@Autowired
	IDestinationManager destinationManager;
	
	@RequestMapping("/api/destination/search*")
	@ResponseBody
	public List<Destination> sreach(@ModelAttribute SearchCondition sc,@ModelAttribute Destination destination) {
		sc.setQueryCondition(destination);
		return destinationManager.search(sc); 
	}
	
	@RequestMapping("/api/destination/query")
    @ResponseBody
    public List<Destination> query(@ModelAttribute Destination cond){
    	return destinationManager.query(cond); 
    }
	
	/**
	 * 查询报名收费导航条地址下拉按钮
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/destination/querychargedes")
	@ResponseBody
	public List<Destination> queryChargeDes(@ModelAttribute Destination cond){
		return destinationManager.queryChargeDes(cond); 
	}
	
	
	@RequestMapping("/api/destination/save")
    @ResponseBody
    public Destination save(@ModelAttribute@ModelFilling Destination destination) {
		String name=destination.getName();
		Destination cond=new Destination();
		cond.setName(name);	
		if(destination.getPkDestination()==null){
			List<Destination> destinations = destinationManager.query(cond);
			//判断名称是否已经存在
			for(int i = 0; i < destinations.size();i++){
				if(destinations.get(i).getName().equals(destination.getName())){
					throw new BusinessException("名称已存在");
				}				
			}
		}
		return destinationManager.save(destination);
    }
		
	
	@RequestMapping("/api/destination/{pkDestination}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkDestination) {
		destinationManager.remove(pkDestination);
	    return new Msg("删除成功");
	}
	
	
}
